package com.sixth.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.CourseDao;
import com.sixth.entity.Course;
import com.sixth.entity.Subject;
import com.sixth.service.CourseService;
import com.sixth.until.CommonResult;
import com.sixth.until.TableResult;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: 老魏
 * @create: 2022-04-15 17:49
 **/
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;

    //查询全部
    @Override
    public TableResult findCourse(Integer pageCurrent, Integer pageSize, Course course) {
        PageHelper.startPage(pageCurrent,pageSize);
        List<Course> course01 = courseDao.findCourse(course.getTitle(),course.getStatus());
        PageInfo<Course> pageInfo = new PageInfo<>(course01);
        return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }
    //更新上下架
    @Override
    public  CommonResult changeStatus(String id ) {
        Course course1 = courseDao.selectById(id);

        System.out.println(id);
        if (course1.getStatus().equals("Normal")){
            Course course = new Course();
            course.setId(id);
            course.setStatus("Draft");
            courseDao.updateById(course);
            return new CommonResult(2000,"下架成功",null);
        }else {
            Course course = new Course();
            course.setId(id);
            course.setStatus("Normal");
            courseDao.updateById(course);
            return new CommonResult(2000,"上架成功",null);
        }
    }

    //修改
    @Override
    public CommonResult updateCourse(Course course) {
        Double price = course.getPrice();
        String id = course.getId();
        String title = course.getTitle();
        String description = course.getDescription();

        if (id==null||id==""){
            String courseId = course.getCourseId();
            int i = courseDao.updateCourse(price,courseId,title,description);
            if (i>0){
                return new CommonResult(2000,"修改成功",null);
            }
        }

        int i = courseDao.updateCourse(price,id,title,description);
        //courseDao.updateById(course);
        return new CommonResult(2000,"修改成功",null);
    }

    //前台课程中心 头部
    @Override
    public CommonResult selectMenu() {
        List<Course> course01 =courseDao.selectMenu();
        return  new CommonResult(2000,"查询成功",course01);
    }

    //前台菜单
    @Override
    public TableResult fontFindCourse(Integer pageCurrent, Integer pageSize, Course course) {
        PageHelper.startPage(pageCurrent,pageSize);

        List<Course> course01 = courseDao.fontFindCourse(course.getTitle(),course.getPrice());

        PageInfo<Course> pageInfo = new PageInfo<>(course01);

        return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public TableResult getCoursByTeacherPhone(Integer pageCurrent, Integer pageSize, String teacherMobile, String checkStatus) {
        PageHelper.startPage(pageCurrent,pageSize);

        List<Course> course01 = courseDao.getCoursByTeacherPhone(teacherMobile,checkStatus);

        PageInfo<Course> pageInfo = new PageInfo<>(course01);

        return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }

}
